from typing import Callable
from nicegui import ui


# 通用代码
def query_elements(selection: Callable[[ui.element], bool]):
    targets = [
        element for element in ui.context.client.elements.values() if selection(element)
    ]

    return targets


def query_element(attr: str):
    targets = query_elements(lambda ele: attr in ele._props)

    if len(targets) == 0:
        return None

    return targets[0]


# 示例
def zone1():
    ui.label("Hello World").props("data-target")
    ui.button("Click me")


def zone2():
    label = query_element("data-target")
    if label is not None:

        @ui.input().on_value_change
        def _(e):
            label.set_text(e.value)


zone1()
zone2()

ui.run()
